#include "stdafx.h"
#include "MayaModel.h"


CMayaModel::CMayaModel(void)
{
}


CMayaModel::~CMayaModel(void)
{
}

void CMayaModel::GetModelFileName(char* cFileName)
{
	bool bDone = false;
	ifstream fin;

	while(bDone == false)
	{
		cin >> cFileName;

		fin.open(cFileName);

		if(fin.good())
		{
			bDone = true;
		}
		else
		{
			fin.clear();
			DEBUG_OUTPUT("Failed to open file");
		}
	}
}

bool CMayaModel::ReadFileCounts(char* cFileName, int& nVertexCount, int& nTextureCount, int& nNormalCount, int& nFaceCount)
{
	ifstream fin;
	char cInput;

	nVertexCount = 0;
	nTextureCount = 0;
	nNormalCount = 0;
	nFaceCount = 0;

	fin.open(cFileName);

	if(fin.fail() == true)
	{
		DEBUG_OUTPUT("Failed to open file");
		return false;
	}

	fin.get(cInput);

	while(fin.eof() == false)
	{
		if(cInput == 'v')
		{
			fin.get(cInput);
			if(cInput == ' ')
			{
				nVertexCount += 1;
			}

			if(cInput == 't')
			{
				nTextureCount += 1;
			}

			if(cInput == 'n')
			{
				nNormalCount += 1;
			}
		}

		if(cInput == 'f')
		{
			fin.get(cInput);
			if(cInput == ' ')
			{
				nFaceCount += 1;
			}
		}

		while(cInput != '\n')
		{
			fin.get(cInput);
		}

		fin.get(cInput);
	}

	fin.close();

	return true;
}

bool CMayaModel::LoadDataStructures(char* cFileName, int nVertexCount, int nTextureCount, int nNormalCount, int nFaceCount)
{
	VertexType* pVertices = nullptr;
	VertexType* pTexCoords = nullptr;
	VertexType* pNormals = nullptr;
	FaceType* pFaceType = nullptr;
	ifstream fin;
	int nVertexIndex = 0;
	int nTexCoordIndex = 0;
	int nNormalIndex = 0;
	int nFaceIndex = 0;
	int nVIndex = 0;
	int nTIndex = 0;
	int nNormIndex = 0;
	char cInput;
	char cInput2;
	ofstream fout;

	pVertices = new VertexType[nVertexCount];
	if(pVertices == nullptr)
	{
		DEBUG_OUTPUT("Failed to create new vertex type pointer");
		return false;
	}

	pTexCoords = new VertexType[nTextureCount];
	if(pTexCoords == nullptr)
	{
		DEBUG_OUTPUT("Failed to create new tex coord type pointer");
		return false;
	}

	pNormals = new VertexType[nNormalCount];
	if(pNormals == nullptr)
	{
		DEBUG_OUTPUT("Failed to create new normal type pointer");
		return false;
	}

	pFaceType = new FaceType[nFaceCount];
	if(pFaceType == nullptr)
	{
		DEBUG_OUTPUT("Failed to create new face type pointer");
		return false;
	}

	fin.open(cFileName);

	if(fin.fail() == true)
	{
		DEBUG_OUTPUT("Failed to open data structure file");
		return false;
	}

	fin.get(cInput);

	while(fin.eof() == false)
	{
		if(cInput == 'v')
		{
			fin.get(cInput);

			if(cInput == ' ')
			{
				fin >> pVertices[nVertexIndex].fXPos >> pVertices[nVertexIndex].fYPos >> pVertices[nVertexIndex].fZPos;

				pVertices[nVertexIndex].fZPos = pVertices[nVertexIndex].fZPos * -1.0f;
				nVertexIndex += 1;
			}

			if(cInput == 't')
			{
				fin >> pTexCoords[nTexCoordIndex].fXPos  >> pTexCoords[nTexCoordIndex].fYPos;

				pTexCoords[nTexCoordIndex].fYPos = 1.0f - pTexCoords[nTexCoordIndex].fYPos;
				nTexCoordIndex += 1;
			}

			if(cInput == 'n')
			{
				fin >> pNormals[nNormalIndex].fXPos >> pNormals[nNormalIndex].fYPos >> pNormals[nNormalIndex].fZPos;

				pNormals[nNormalIndex].fZPos = pNormals[nNormalIndex].fZPos * -1.0f;
				nNormalIndex += 1;
			}
		}

		if(cInput == 'f')
		{
			fin.get(cInput);
			if(cInput == ' ')
			{
				fin >> pFaceType[nFaceIndex].nVIndex3 >> cInput2 >> pFaceType[nFaceIndex].nTIndex3 >> cInput2 >> pFaceType[nFaceIndex].nIndex3
					>> pFaceType[nFaceIndex].nVIndex2 >> cInput2 >> pFaceType[nFaceIndex].nTIndex2 >> cInput2 >> pFaceType[nFaceIndex].nIndex2
					>> pFaceType[nFaceIndex].nVIndex1 >> cInput2 >> pFaceType[nFaceIndex].nTIndex1 >> cInput2 >> pFaceType[nFaceIndex].nIndex1;

				nFaceIndex += 1;
			}
		}

		while(cInput != '\n')
		{
			fin.get(cInput);
		}

		fin.get(cInput);
	}

	fin.close();

	fout.open("../Resources/Models/Maya/model.txt");

	fout << "Vertex Count: " << (nFaceCount * 3) << "\n\n";
	fout << "Data: " << "\n\n";

	for(int i = 0; i < nFaceIndex; ++i)
	{
		nVIndex = pFaceType[i].nVIndex1 - 1;
		nTIndex = pFaceType[i].nTIndex1 - 1;
		nNormIndex = pFaceType[i].nIndex1 - 1;

		fout << pVertices[nVIndex].fXPos << ' ' << pVertices[nVIndex].fYPos << ' ' << pVertices[nVIndex].fZPos << ' '
			<< pTexCoords[nTIndex].fXPos << ' ' << pTexCoords[nTIndex].fYPos << ' '
			<< pNormals[nNormIndex].fXPos << ' ' << pNormals[nNormIndex].fYPos << ' ' << pNormals[nNormIndex].fZPos << '\n';

		nVIndex = pFaceType[i].nVIndex2 - 1;
		nTIndex = pFaceType[i].nTIndex2 - 1;
		nNormIndex = pFaceType[i].nIndex2 - 1;

		fout << pVertices[nVIndex].fXPos << ' ' << pVertices[nVIndex].fYPos << ' ' << pVertices[nVIndex].fZPos << ' '
			<< pTexCoords[nTIndex].fXPos << ' ' << pTexCoords[nTIndex].fYPos << ' '
			<< pNormals[nNormIndex].fXPos << ' ' << pNormals[nNormIndex].fYPos << ' ' << pNormals[nNormIndex].fZPos << '\n';

		nVIndex = pFaceType[i].nVIndex3 - 1;
		nTIndex = pFaceType[i].nTIndex3 - 1;
		nNormIndex = pFaceType[i].nIndex3 - 1;

		fout << pVertices[nVIndex].fXPos << ' ' << pVertices[nVIndex].fYPos << ' ' << pVertices[nVIndex].fZPos << ' '
			<< pTexCoords[nTIndex].fXPos << ' ' << pTexCoords[nTIndex].fYPos << ' '
			<< pNormals[nNormIndex].fXPos << ' ' << pNormals[nNormIndex].fYPos << ' ' << pNormals[nNormIndex].fZPos << '\n';
	}

	fout.close();

	if(pVertices != nullptr)
	{
		delete[] pVertices;
		pVertices = nullptr;
	}

	if(pTexCoords != nullptr)
	{
		delete[] pTexCoords;
		pTexCoords = nullptr;
	}

	if(pNormals != nullptr)
	{
		delete[] pNormals;
		pNormals = nullptr;
	}

	if(pFaceType != nullptr)
	{
		delete[] pFaceType;
		pFaceType = nullptr;
	}

	return true;
}

bool CMayaModel::LoadModel(char* cFileName)
{
	bool bResult = false;
	int nVertexCount = 0;
	int nTextureCount = 0;
	int nNormalCount = 0;
	int nFaceCout = 0;

	//GetModelFileName(cFileName);

	bResult = ReadFileCounts(cFileName, nVertexCount, nTextureCount, nNormalCount, nFaceCout);
	if(bResult == false)
	{
		DEBUG_OUTPUT("Failed to read file counts");
		return false;
	}

	bResult = LoadDataStructures(cFileName, nVertexCount, nTextureCount, nNormalCount, nFaceCout);
	if(bResult == false)
	{
		DEBUG_OUTPUT("Failed to load data structures");
		return false;
	}
	
	return true;
}
